<?php

namespace app\data\controller;

use cmf\controller\AdminBaseController;

class CateController extends AdminBaseController
{
    function index()
    {
        return $this->fetch();
    }

    /**
     * 获取数据
     */
    function getdata()
    {
        $user = get_access_user();

        $start_time = $this->request->post('start_time', 0, 'strtotime');
        $end_time = $this->request->post('end_time', 0, 'strtotime');
        $type = $this->request->post('type', 'cate');
        if (!$end_time) {
            if ($start_time) {
                $end_time = time() + 86399;
            }
        } else {
            $end_time += 86399;
        }

        switch ($type) {
            case 'follow':
                $result = $this->getcatefollow($user, $start_time, $end_time);
                break;
            default:
                $result = $this->getcate($user, $start_time, $end_time);
                break;


        }
        $this->result($result, 200);
    }

//    private function datalist()
//    {
//        $array = [
//            [
//                'name' => 'LicensePlate',
//                'title' => '卖家'
//            ],
//            [
//                'name' => 'Buyer',
//                'title' => '买家'
//            ],
//            [
//                'name' => 'Shoudian',
//                'title' => '售电公司'
//            ],
//            [
//                'name' => 'Medium',
//                'title' => '中介'
//            ],
//            [
//                'name' => 'Follow',
//                'title' => '跟进'
//            ],
//            [
//                'name' => 'VirtualAddress',
//                'title' => '虚拟地址'
//            ],
//        ];
//        $result = [];
//        foreach ($array as $val) {
//            if (!auth_check('index', "business/{$val['name']}/")) {
//                continue;
//            }
//            $name = '\\app\\data\\model\\' . $val['name'] . 'Model';
//            $model = new $name();
//            $user = get_access_user();
//            if(!method_exists($model,'getCount')) {
//                continue;
//            }
//            $data = $model->getCount($user);
//            $data['title'] = $val['title'];
//            $result[$val['name']] = $data;
//        }
//        return $result;
//    }

    /**
     * 获取分类数据
     * @param string $users
     * @param int $start_time
     * @param int $end_time
     * @return array
     */
    private function getcate($users = '', $start_time = 0, $end_time = 0)
    {
        $array = [
            [
                'name' => 'LicensePlate',
                'title' => '卖家'
            ],
//            [
//                'name' => 'Buyer',
//                'title' => '买家'
//            ],
//            [
//                'name' => 'Shoudian',
//                'title' => '售电公司'
//            ],
//            [
//                'name' => 'Medium',
//                'title' => '中介'
//            ],
//            [
//                'name' => 'Follow',
//                'title' => '跟进'
//            ],
//            [
//                'name' => 'VirtualAddress',
//                'title' => '虚拟地址'
//            ],
        ];
        $result = [];
        foreach ($array as $val) {
            if (!auth_check('index', "business/{$val['name']}/")) {
                continue;
            }
            $name = '\\app\\data\\model\\' . $val['name'] . 'Model';
            $model = new $name();
            $user = get_access_user();
            if (!method_exists($model, 'getCate')) {
                continue;
            }
            $data = $model->getCate($user, $start_time, $end_time);
            $result[$val['name']] = [
                'data' => $data,
                'title' => $val['title']
            ];
        }
        return $result;
    }

    /**
     * 获取分类跟进数据
     * @param string $users
     * @param int $start_time
     * @param int $end_time
     * @return array
     */
    private function getcatefollow($users = '', $start_time = 0, $end_time = 0)
    {
        $array = [
//            [
//                'name' => 'LicensePlate',
//                'title' => '卖家'
//            ],
            [
                'name' => 'Buyer',
                'title' => '买家'
            ],
//            [
//                'name' => 'Shoudian',
//                'title' => '售电公司'
//            ],
//            [
//                'name' => 'Medium',
//                'title' => '中介'
//            ],
//            [
//                'name' => 'Follow',
//                'title' => '跟进'
//            ],
//            [
//                'name' => 'VirtualAddress',
//                'title' => '虚拟地址'
//            ],
        ];
        $result = [];
        foreach ($array as $val) {
            if (!auth_check('index', "business/{$val['name']}/")) {
                continue;
            }
            $name = '\\app\\data\\model\\' . $val['name'] . 'Model';
            $model = new $name();
            $user = get_access_user();
            if (!method_exists($model, 'getCate')) {
                continue;
            }
            $data = $model->getCateFollow($user, $start_time, $end_time);
            $result[$val['name']] = [
                'data' => $data,
                'title' => $val['title']
            ];
        }
        return $result;
    }

    function follow()
    {
        return $this->fetch();
    }
}